package com.bianmaba.controllers.base.support;

import com.bianmaba.commons.bean.result.OperationResult;
import com.bianmaba.commons.bean.result.QueryResult;
import com.bianmaba.commons.data.jpa.domain.request.PageableCondition;
import com.bianmaba.commons.data.jpa.domain.request.PageableRequest;
import com.bianmaba.spring.mvc.annotation.RequestResponseBodyConfig;
import com.bianmaba.spring.mvc.config.annotation.EntityParam;
import com.bianmaba.spring.web.controller.ReadController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.List;

@Api(
        description = "只读API接口"
)
public abstract class ReadControllerSwaggerSupport<T, ID extends Serializable> extends ReadController<T, ID> {

    @ApiOperation("根据数据ID获取数据，支持POST请求，接收FormData数据(Content-type=application/x-www-form-urlencoded)和QueryString")
    @RequestResponseBodyConfig
    @RequestMapping(value = {"/get_by_id"}, method = RequestMethod.POST)
    public OperationResult<T> getById(ID id) {
        return super.getById(id);
    }

    @ApiOperation("根据所有数据，支持POST请求。")
    @RequestResponseBodyConfig
    @RequestMapping(value = {"/all"}, method = RequestMethod.POST)
    public QueryResult<List<T>> all() {
        return super.all();
    }


    @RequestMapping(value = {"/query"}, method = RequestMethod.POST)
    @RequestResponseBodyConfig
    public QueryResult<List<T>> query(@EntityParam LinkedHashMap<String, Object> params, @EntityParam PageableRequest pageable) {
        return super.query(params, pageable);
    }

    @ApiOperation("数据查询接口，支持POST请求，接收JSON数据Content-type=application/json")
    @RequestResponseBodyConfig
    @RequestMapping(
            value = {"/query"},
            headers = {"Content-Type=application/json"},
            method = RequestMethod.POST
    )
    public QueryResult<List<T>> query(@RequestBody PageableCondition condition) {
        return super.query(condition);
    }
}
